package com.xxl.job.executor.mybatis.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxl.job.executor.mybatis.entity.JobUser;
import com.xxl.job.executor.mybatis.service.JobUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (JobUser)表控制层
 *
 * @author makejava
 * @since 2020-07-13 15:54:52
 */
@RestController
@RequestMapping("jobUser")
public class JobUserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private JobUserService jobUserService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jobUser 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JobUser> page, JobUser jobUser) {
        return success(this.jobUserService.page(page, new QueryWrapper<>(jobUser)));
    }

    /**
     * 自定义分页查询
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/selectByPage")
    public R selectByPage(@PathParam("current") Integer current, @PathParam("current")  Integer size){
        return success(jobUserService.selectByPage(current,size));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.jobUserService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jobUser 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody JobUser jobUser) {
        List<JobUser> jobUsers = new ArrayList<>();
        jobUsers.add(jobUser);
        jobUsers.add(jobUser);
        this.jobUserService.insertBatch(jobUsers);
        return success(this.jobUserService.save(jobUser));
    }

    /**
     * 修改数据
     *
     * @param jobUser 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody JobUser jobUser) {
        return success(this.jobUserService.updateById(jobUser));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.jobUserService.removeByIds(idList));
    }
}